
public class ReentrantLock {

    private boolean isLocked = false;
    private Thread lockedBy = null;

    public synchronized void lock() {
        try {
            while (isLocked && Thread.currentThread() != lockedBy) {
                this.wait();
            }
            isLocked = true;
            lockedBy = Thread.currentThread();
        } catch (InterruptedException e) {
            System.out.println("Lock interrupted " + e);
        }
    }

    public synchronized void unlock() {
        this.notifyAll();
        isLocked = false;
        lockedBy = null;
    }

}
